package cn.t.repository.impl.user;

import cn.t.converter.user.RoleDeviceConverter;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.model.user.RoleDeviceDO;
import cn.t.persistence.entity.user.RoleDevicePO;
import cn.t.persistence.mapper.user.RoleDeviceMapper;
import cn.t.repository.user.RoleDeviceRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 部门信息表数据仓库
 * @author l
 */
@Slf4j
@Repository
@AllArgsConstructor
public class RoleDeviceRepositoryImpl extends RepositoryImpl<RoleDeviceMapper, RoleDevicePO, RoleDeviceConverter, RoleDeviceDO> implements RoleDeviceRepository {


    @Override
    public List<RoleDeviceDO> findByModel(RoleDeviceDO roleDeviceDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ? " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<RoleDevicePO> list = getWrapper(roleDeviceDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<RoleDevicePO> getWrapper(RoleDeviceDO roleDeviceDO) {
        LambdaQueryChainWrapper<RoleDevicePO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(roleDeviceDO.getGmtCreatedStart()) && !ObjectUtils.isEmpty(roleDeviceDO.getGmtCreatedEnd())) {
            wrapper.between(RoleDevicePO::getGmtCreated, roleDeviceDO.getGmtCreatedStart(), roleDeviceDO.getGmtCreatedEnd());
        }
        if (!ObjectUtils.isEmpty(roleDeviceDO.getGmtModifiedStart()) && !ObjectUtils.isEmpty(roleDeviceDO.getGmtModifiedEnd())) {
            wrapper.between(RoleDevicePO::getGmtModified, roleDeviceDO.getGmtModifiedStart(), roleDeviceDO.getGmtModifiedEnd());
        }
        if (!ObjectUtils.isEmpty(roleDeviceDO.getDeviceId())) {
            wrapper.eq(RoleDevicePO::getDeviceId, roleDeviceDO.getDeviceId());
        }
        if (!ObjectUtils.isEmpty(roleDeviceDO.getRoleId())) {
            wrapper.eq(RoleDevicePO::getRoleId, roleDeviceDO.getRoleId());
        }

        if (!ObjectUtils.isEmpty(roleDeviceDO.getStatus())) {
            wrapper.eq(RoleDevicePO::getStatus, roleDeviceDO.getStatus());
        }
        return wrapper;
    }

    @Override
    public long count(RoleDeviceDO roleDeviceDO) {
        return getWrapper(roleDeviceDO).count();
    }

    @Override
    public boolean removeByRoleId(Long roleId) {
        return getBaseMapper().removeByRoleId(roleId);
    }

    @Override
    public boolean removeById(Long id) {
        return getBaseMapper().removeById(id);
    }
}
